package com.jshcbd.mvp_kt_demo.rxjava

import com.jshcbd.mvp_kt_demo.base.mvp.BaseView
import io.reactivex.Observable
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

object RxTransformer {
    /**
     * 无参数  仅做切换线程
     *
     * @param <T> 泛型
     * @return 返回Observable
    </T> */
    fun <T> transform(): ObservableTransformer<T, T> {
        return ObservableTransformer<T, T> { upstream: Observable<T> ->
            upstream
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
        }
    }

    /**
     * 界面请求，不需要加载和隐藏loading时调用 使用RxLifeCycle
     * 传入view接口，Activity，Fragment等实现了view接口，Activity，Fragment继承于[com.trello.rxlifecycle2.components.support.RxAppCompatActivity]
     * 也就实现了bindToLifecycle方法
     *
     * @param view View
     * @param <T>  泛型
     * @return
    </T> */
    fun <T> transform(view: BaseView): ObservableTransformer<T, T> {
        return ObservableTransformer<T, T> { observable: Observable<T> ->
            observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(view.bindToLifecycle())
        }
    }

    /**
     * 界面请求，需要加载和隐藏loading时调用,使用RxLifeCycle
     * 传入view接口，Activity，Fragment等实现了view接口，Activity，Fragment继承于[com.trello.rxlifecycle2.components.support.RxAppCompatActivity]
     * 也就实现了bindToLifecycle方法
     *
     * @param view View
     * @param <T>  泛型
     * @return
    </T> */
    fun <T> transformWithLoading(view: BaseView): ObservableTransformer<T, T> {
        return ObservableTransformer<T, T> { observable: Observable<T> ->
            observable
                .subscribeOn(Schedulers.io())
                .doOnSubscribe {
                    view.showLoading() //显示进度条
                }
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally {
                    view.hideLoading() //隐藏进度条
                }.compose(view.bindToLifecycle())
        }
    }
}